/**
 * Created by xw on 2018/03/21
 */

export default new class Uri {
  constructor() {
  }

  //URI 解码
  decode(value) {
    return decodeURIComponent(value)
  }

  //URI 编码
  encode(value) {
    return encodeURIComponent(value)
  }

  //对象转为url
  jointUrl(obj) {  
    let paramArr = [];  
    let param = '';  
    if (obj && typeof obj === 'object') {  

        for (const i in obj) {  
          if (obj.hasOwnProperty(i)) {
              if (obj[i] !== '' && obj[i] !== null && typeof obj[i] !== 'undefined') {
                paramArr.push(encodeURIComponent(i))  
                paramArr.push('=')
                paramArr.push(encodeURIComponent(obj[i]))  
                paramArr.push('&') 
              }  
          }  
        }
      }  
    param = paramArr.join('').replace(/&$/, '')
    return param;  
  }  

  //url转为对象
  queryURL(url){
    let newurl = url ? url : window.location.href
    let arr = newurl.split("?")
    let params = arr[1].split("&")
    let obj = {}
    for(var i=0; i<params.length; i++){
        let param = params[i].split("=")
        obj[param[0]] = decodeURIComponent(param[1])
    }
    return obj
  }

  isUrl (str) {
    let durl = /https?:\/\/([^\/]+)/i  
    return durl.test(str)
  }
  //相对路径
  domainURI(str){  
    let path = ''
    let domain = ''
    let durl = /https?:\/\/([^\/]+)/i  
    if(!durl.test(str)){
      return str
    }

    domain = str.match(durl)
    path = str.split(domain[0])[1]

    return path
   }



}